//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		var mailNo=thisform.find('input[name=mailNo]').val();
		var receiveMan=thisform.find('input[name=receiveMan]').val();
		var print_status=thisform.find('input[name=print_status]').val();
		var start_time=thisform.find('input[name=start_time]').val();
		var end_time=thisform.find('input[name=end_time]').val();
		var logiscompany_name=thisform.find('input[name=logiscompany_name]').val();
		
		var goods_name=thisform.find('input[name=goods_name]').val();
		var nums=thisform.find('input[name=nums]').val();
		var check_cate=thisform.find('input[name=check_cate]').val();
		var pick_status=thisform.find('input[name=pick_status]').val();
		var is_delivery=thisform.find('input[name=is_delivery]').val();
		$('#printorderDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#printorderDatagrid').datagrid({ 
			queryParams:{
				order_no:order_no,
				mailNo:mailNo,
				receiveMan:receiveMan,
				start_time:start_time,
				end_time:end_time,
				print_status:print_status,
				goods_name:goods_name,
				nums:nums,
				check_cate:check_cate,
				pick_status:pick_status,
				is_delivery:is_delivery,
				logiscompany_name:logiscompany_name
			}
		});
	})
	
	$('.AdvanceSearch').click(function(){
		$('#printAdvanceSearch').toggle(100);
	})

	$('#printorderDatagrid').datagrid({
		//url:getprintorderUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getprintorderUrl,
		title:'紧急订单',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		// {field:'ids',title:'',
				// formatter:function(value,rowData,index){
					// turl=scanorder_url;
					// turl=turl+"?mailNo="+rowData.logistics_no;
					// return "<a href='"+turl+"'>复核</a>";
				// }
		// },
		{field:'order_no',title:'订单编号',sortable:true},
		{field:'logistics_no',title:'电子面单号',sortable:true},
		{field:'logiscompany_name',title:'快递公司',sortable:true},
		{field:'user_name',title:'收件人姓名',sortable:true},
		{field:'user_phone',title:'收件人电话',sortable:true},
		{field:'province',title:'省',sortable:true},
		{field:'city',title:'市',sortable:true},
		{field:'area',title:'区',sortable:true},
		{field:'user_address',title:'收件人地址',width:'100px',
				formatter:function(val){
					return "<span title='"+val+"' alt='"+val+"' style='cursor:pointer;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:100px;float:left;'>"+val+"</span>";
				}
		},
		{field:'mark_mailno',title:'标签'},
		{field:'erp_mark_mailno',title:'erp备注'},
		{field:'pick_status',title:'已制单',sortable:true,
				formatter:function(value,rowData,index){
					if(value==0){
						return '否';
					}
					if(value==1){
						htmlobj=$.ajax({type:'post',url:get_picklist,data:{order_id:rowData.id},async:false});//ajax动态获取物流备案公司名称
						var info=eval('('+htmlobj.responseText+')');
						return info.message;
					}
				}
		},
		{field:'is_delivery',title:'已出库',sortable:true,
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(val==0){
						return '否';
					}
					if(val==1){
						return '是';
					}
			    }},
		{field:'times',title:'打印次数',sortable:true,
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(val!=0){
						return '(操作人:'+rowData.printer_name+')'+val;
					}
			    }
		},
		{field:'add_time',title:'推送时间',sortable:true},
		{field:'a.order_no',title:'商品编码',
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(rowData.goods_list==null){
						return '没有商品详情';
					}
					var header="<table style='width:100%;'>";
					var footer="</table>";
					var body="";
					for(i=0;i<rowData.goods_list.length;i++){
						body=body+"<tr><td style='border:0px;'>"+rowData.goods_list[i].goods_no+"</td></tr>";
					}
					var text=header+body+footer;
					return text;
			    }
		},
		{field:'a.times',title:'商品名称',
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(rowData.goods_list==null){
						return '没有商品详情';
					}
					var header="<table style='width:100%;'>";
					var footer="</table>";
					var body="";
					for(i=0;i<rowData.goods_list.length;i++){
						body=body+"<tr><td title='"+rowData.goods_list[i].goods_name+"' alt='"+rowData.goods_list[i].goods_name+"' style='border:0px;cursor:pointer;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:200px;float:left;'>"+rowData.goods_list[i].goods_name+"</td></tr>";
					}
					// body=body+"<tr height=10 border=0><td colspan='3'></td></tr>";
					var text=header+body+footer;
					return text;
			    }
		},
		{field:'b.times',title:'数量',
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(rowData.goods_list==null){
						return '没有商品详情';
					}
					var header="<table style='width:100%;'>";
					var footer="</table>";
					var body="";
					for(i=0;i<rowData.goods_list.length;i++){
						body=body+"<tr><td style='border:0px;'>"+rowData.goods_list[i].goods_nums+"</td></tr>";
					}
					// body=body+"<tr height=10 border=0><td colspan='3'></td></tr>";
					var text=header+body+footer;
					return text;
			    }
		}
		]],
		view: detailview,
		detailFormatter: function(rowIndex,rowData){
			// console.log(rowData);
			if(rowData.goods_list==null){
				return '没有商品详情';
			}
			var header="<table style='width:50%;margin-bottom:10px;'><tr style='background:#F5F5F5'><td>商品编码</td><td>商品名称</td><td>数量</td></tr>";
			var footer="</table>";
			var body="";
			for(i=0;i<rowData.goods_list.length;i++){
				body=body+"<tr><td>"+rowData.goods_list[i].goods_no+"</td><td>"+rowData.goods_list[i].goods_name+"</td><td>"+rowData.goods_list[i].goods_nums+"</td></tr>";
			}
			// body=body+"<tr height=10 border=0><td colspan='3'></td></tr>";
			var text=header+body+footer;
			return text;
		},
		expand:true,
		onBeforeLoad:function(data){
			// console.log(data);
			data.first_orderid=$('#first_orderid').val();
			data.is_emergency=1;
        },
		onLoadSuccess: function (data) {
		  first_orderid=eval(data).first_orderid;
		  $('#first_orderid').val(first_orderid);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
			if (row.is_delivery==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
printorderObj={
	showAll:function(){//显示默认条件下所有数据
		$('#first_orderid').val('');
		$('#printorderDatagrid').datagrid({ queryParams:''});
	},
	showAdvanceBar:function(){
		$('#printorderDatagrid').datagrid({ queryParams:{show_advance:1}});
	},
	removeBar:function(){
		var storageRow=$('#printorderDatagrid').datagrid('getSelections');
        if (storageRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+storageRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<storageRow.length;i++){
                		ids.push(storageRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(DelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){     
								$('#printorderDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}